import csv
import os.path
import time
from selenium import webdriver
from selenium.webdriver.support.select import Select

def save_province_name():
    driver.get("https://yz.chsi.com.cn/zsml/queryAction.do")
    province = driver.find_element_by_id("ssdm")  # 省市
    province_list = Select(province)
    for option in province_list.options:
        province_name.append(option.text)

def find_data(s):
    for zhuanye_num in range(1, 14):  # 遍历13个专业
        driver.get("https://yz.chsi.com.cn/zsml/queryAction.do")
        province = driver.find_element_by_id("ssdm")  # 省市
        Select(province).select_by_index(s)
        menlei = driver.find_element_by_id("mldm")  # 门类/专业学位
        Select(menlei).select_by_index(1)
        zyly = driver.find_element_by_id("yjxkdm")  # 专业领域/(21/0854)电子信息
        Select(zyly).select_by_index(21)
        zuanye = driver.find_element_by_id("zymc")  # 13个专业
        Select(zuanye).select_by_index(zhuanye_num)
        xxfs = driver.find_element_by_name("xxfs")  # 学习方式：全日制
        Select(xxfs).select_by_index(1)
        driver.find_element_by_name('button').click()  # 查询按钮
        school_urls = driver.find_elements_by_xpath(
            '//*[@id="form3"]/a')  # 学校链接
        school_url = []
        for i in school_urls:
            school_url.append(i.get_attribute('href'))
            # print('school_url:\n', len(school_url))
        for n in range(0, len(school_url)):
            # time.sleep(6)
            driver.get(school_url[n])  # 进入学校各学院
            colleges = driver.find_elements_by_xpath(
                '/html/body/div[2]/div[3]/div/div[2]/table/tbody/tr')  # 一个学校的所有学院
            each_college(colleges)  # 循环每个学院
        save_data()  # 保存一个专业的所有学校
        # time.sleep(2)
        list_clear()
    # driver.quit()

def each_college(colleges):  # 循环每个学院
    for each in range(1, len(colleges)+1):
        str_each = str(each)
        # time.sleep(6)
        driver.find_element_by_xpath(
            '/html/body/div[2]/div[3]/div/div[2]/table/tbody/tr['+str_each+']/td[8]/a').click()  # 进入详细页面
        windows = driver.window_handles
        data_details(windows)

def list_clear():
    school_name.clear()
    test_methd.clear()
    yuanxi.clear()
    zy.clear()
    research.clear()
    teacher.clear()
    students.clear()
    notes.clear()
    examinations.clear()

def data_details(windows):  # 获取详细页面数据
    driver.switch_to.window(windows[1])
    school_name.append(driver.find_element_by_xpath(
        './/table/tbody/tr[1]/td[2]').text)
    test_methd.append(driver.find_element_by_xpath(
        './/table/tbody/tr[1]/td[4]').text)
    yuanxi.append(driver.find_element_by_xpath(
        './/table/tbody/tr[2]/td[2]').text)
    zy.append(driver.find_element_by_xpath('.//table/tbody/tr[2]/td[4]').text)
    research.append(driver.find_element_by_xpath(
        './/table/tbody/tr[3]/td[4]').text)
    teacher.append(driver.find_element_by_xpath(
        './/table/tbody/tr[4]/td[2]').text)
    students.append(driver.find_element_by_xpath(
        './/table/tbody/tr[4]/td[4]').text)
    notes.append(driver.find_element_by_xpath(
        './/table/tbody/tr[5]/td[2]').text)

    obj1 = driver.find_element_by_xpath(
        '/html/body/div[2]/div[3]/div[1]/div[1]/div/table/tbody/tr/td[1]').text
    obj2 = driver.find_element_by_xpath(
        '/html/body/div[2]/div[3]/div[1]/div[1]/div/table/tbody/tr/td[2]').text
    obj3 = driver.find_element_by_xpath(
        '/html/body/div[2]/div[3]/div[1]/div[1]/div/table/tbody/tr/td[3]').text
    obj4 = driver.find_element_by_xpath(
        '/html/body/div[2]/div[3]/div[1]/div[1]/div/table/tbody/tr/td[4]').text
    examinations.append(obj1.split(
        "\n")[0]+obj2.split("\n")[0]+obj3.split("\n")[0]+obj4.split("\n")[0])
    driver.switch_to.window(windows[1])
    driver.close()
    driver.switch_to.window(windows[0])

def save_data():
    if(len(zy) != 0):
        with open(os.path.join(path_csv, csv_name), 'a', newline='') as f:
            csv_write = csv.writer(f)
            csv_write.writerow([f'{zy[0]}'])
            csv_write.writerow(
                ['招生单位', '院系所', '专业', '研究方向', '考试方式', '考试科目', '招录人数', '指导老师', '备注'])
        for i in range(0, len(zy)):
            print("招生单位:", school_name[i])
            print("院系所:", yuanxi[i])
            print("专业:", zy[i])
            print("研究方向:", research[i])
            print("考试方式:", test_methd[i])
            print("考试科目:", examinations[i])
            print("招录人数:", students[i].split(
                "：")[1].split("(")[0])
            print("指导老师:", teacher[i])
            print("备注:", notes[i])

            with open(os.path.join(path_csv, csv_name), 'a', newline='') as f:
                csv_write = csv.writer(f)
                # csv_write.writerow([zy[i]])
                csv_write.writerow([school_name[i], yuanxi[i], zy[i], research[i], test_methd[i], examinations[i], students[i].split(
                    "：")[1].split("(")[0], teacher[i], notes[i]])
            # f.close()

if __name__ == '__main__':
    option = webdriver.ChromeOptions()
    option.add_argument('headless')
    driver = webdriver.Chrome(options=option)  # 浏览器驱动,不打开浏览器运行

    # driver = webdriver.Chrome()  # 打开浏览器

    province_name = []  # 省/市名
    school_name = []  # 学校名
    test_methd = []  # 考试方式
    yuanxi = []  # 院系
    zy = []  # 专业
    research = []  # 研究方向
    teacher = []  # 指导老师
    students = []  # 招收人数
    notes = []  # 备注
    examinations = []  # 考试科目
    save_province_name()  # 保存省名
    path_csv = r"D:\桌面\考研大数据\数据分析\data"  # 保存路径
    csv_name = ''  # 文件名
    for s in range(1, 32):  # 遍历31个省份
        page = 0
        csv_name = f'{province_name[s]}.csv'  # 保存文件名称
        find_data(s)
